<?php
/**
 * @author Truong Kim Phung
 * @copyright 2014
 * @description: save update account
 */
// not login
// not perm add
if(!isset($AppUI->id)) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
// data input
$intAdminId         = $this->input->post('padminid', 0);
$strUsername        = $this->input->post('pusername',null); 
$strPassword    	= $this->input->post('ppassword',null);			
$strConfirmPassword = $this->input->post('ppassword_confirm',null);
$strFullname        = $this->input->post('pfullname', null);
$intType            = (int)$this->input->post('ptype', 0);
$intRoomId          = (int)$this->input->post('proomid', 0);
$arrErr             = array();
// strip tag data input
$lenPassword        = strlen($strPassword);
$strFullname        = CI_Function::stripTag($strFullname);
$lenFullname        = strlen($strFullname);
// validate manual	
//+ password
if(isset($strPassword) && !empty($strPassword) && $lenPassword < VL_PASSWORD_MINLENGTH) 
    array_push($arrErr, $arrLocale['password_minlength']);
elseif(isset($strPassword) && !empty($strPassword) && $strPassword != $strConfirmPassword)
    array_push($arrErr, $arrLocale['password_notmatch']);
//+ confirm password
if(!empty($strPassword) && (!isset($strConfirmPassword) || empty($strConfirmPassword)))
    array_push($arrErr, $arrLocale['password_confirm_required']);

if(isset($arrErr) && count($arrErr) == 0) {//allow save
    // unset session if exists
    if(isset($_SESSION['ADMIN_ERROR'])) unset($_SESSION['ADMIN_ERROR']);
    if(isset($_SESSION['ADMIN_POST'])) unset($_SESSION['ADMIN_POST']);
    $this->load->model('admin_model', 'admin');	    
    $strPassword = (isset($strPassword) && !empty($strPassword)) ? md5($strPassword) : '';
    $intError = $this->admin->update($AppUI->id, $intAdminId, $strUsername, $strPassword, $strFullname, 
                                     $intType, $intRoomId);
    if($intError==0) CI_Function::setMsg($arrMessage['save_update']);
    elseif($intError==1) array_push($arrErr, $arrError['noperm_update']);        
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) {
    $_SESSION['ADMIN_ERROR']    = $arrErr;    
    $_SESSION['ADMIN_POST']     = $this->input->post();
    
    redirect(HOST_BACKEND . 'admin/edit?adminid=' . $intAdminId);
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
